package com.blue.controller;

import com.blue.example.Product;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

/**
 * Created by Administrator on 2019/8/4.
 */
public class ChangeConfigController implements Initializable {

    public static String tomcatport;

    public static String tomcatpath;

    public static ArrayList<String> webList;

    public static StringBuffer selectFlags;

    private GridPane gridPane = null;

    private Product product = null;

    private int count ;
    @FXML
    private Button projectNext;
    @FXML
    private AnchorPane projectAnchorPane;
    @FXML
    private Line projectLine1;
    @FXML
    private Line projectLine2;
    @FXML
    private Line projectLine3;
    @FXML
    private Line projectLine4;
    @FXML
    private Line projectLine5;
    @FXML
    private Line projectLine6;
    @FXML
    private Circle projectCircle1;
    @FXML
    private Circle projectCircle2;
    @FXML
    private Circle projectCircle3;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //step process
        projectLine1.setStroke( Color.GREEN );
        projectLine1.setStrokeWidth( 4.0 );
        projectCircle1.setFill( Color.GREEN );
        projectLine2.setStroke( Color.GREEN );
        projectLine2.setStrokeWidth( 4.0 );
        projectLine3.setStroke( Color.GREEN );
        projectLine3.setStrokeWidth( 4.0 );
        projectCircle2.setFill( Color.GREEN );
        projectLine4.setStroke( Color.GREEN );
        projectLine4.setStrokeWidth( 4.0 );
        projectLine5.setStroke( Color.GREEN );
        projectLine5.setStrokeWidth( 4.0 );
        projectCircle3.setFill( Color.GREEN );
        projectLine6.setStroke( Color.GREEN );
        projectLine6.setStrokeWidth( 4.0 );

        webList = ChangeProjectController.webList;
        tomcatpath = TomcatController.tomcatpath;
        tomcatport = TomcatController.tomcatport;
        selectFlags = ChangeProjectController.selectFlags;
        System.out.println( "ChangeProjectController. selectFlags :" + ChangeProjectController.selectFlags.toString() );
        product = new Product();
        product.setSelectFlags( selectFlags );
        product.setTomcatpath( tomcatpath );
        product.setTomcatPort( tomcatport );
        product.setWebList( webList );
        System.out.println( "TomcatController. selectFlags :" + product.selectFlags.toString() );
        gridPane = product.addConfig();
        AnchorPane.setTopAnchor( gridPane, 10.0 );
        projectAnchorPane.getChildren().addAll( gridPane );
    }

    public void backTomcat(ActionEvent actionEvent) {
        /*projectAnchorPane.getChildren().removeAll( gridPane );
        GridPane gridPane = product.addTomcat( "Tomcat" );
        AnchorPane.setTopAnchor( gridPane, 10.0 );
        projectAnchorPane.getChildren().addAll( gridPane );*/
        //获取当前主界面舞台
        Stage mainStage = (Stage) projectAnchorPane.getScene().getWindow();
        //读入布局
        FXMLLoader fxmlLoader = new FXMLLoader( getClass().getResource( "/fxml/tomcat.fxml" ) );
        BorderPane root = null;
        try {
            root = fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = projectAnchorPane.getScene();
        scene.setRoot( root );
        scene.getStylesheets().add( "/css/style.css" );
        mainStage.setTitle( "Product Installation System of BLUE" );
        mainStage.setMaximized( true );
        mainStage.setScene( scene );
        mainStage.getIcons().add( new Image( "/img/logo.png" ) );
        //mainStage.show();
    }

    public void installWeb(ActionEvent actionEvent) {

        count = product.count;
        if (0 == count || 3 == count) {
            product.installation( "config" );
        }
        if(4 == count){
            Alert alert = new Alert( Alert.AlertType.WARNING );
            alert.setTitle( "提示" );
            alert.setContentText( "当前项目正在安装，请不要重复点击!" );
            alert.showAndWait();
        }
        if (5 == count){
            Alert alert = new Alert( Alert.AlertType.WARNING );
            alert.setTitle( "提示" );
            alert.setContentText( "当前项目已经安装成功，请不要重复安装!" );
            alert.showAndWait();
            projectNext.setText( "安装完成" );
            projectNext.setDisable( true );
        }
    }
}
